package com.dianjing.dianjing.controller;


import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.dianjing.dianjing.dto.input.Evaluate;
import com.dianjing.dianjing.dto.input.OrderInput;
import com.dianjing.dianjing.dto.output.PainteHistoryOrderOutput;
import com.dianjing.dianjing.entity.Order;
import com.dianjing.dianjing.enums.OrderOpenStateEnum;
import com.dianjing.dianjing.enums.OrderStateEnum;
import com.dianjing.dianjing.service.OrderService;
import com.dianjing.dianjing.util.DataUtils;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.BeanUtils;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;
import java.util.stream.Collectors;

/**
 * <p>
 * 订单表 前端控制器
 * </p>
 *
 * @author LBG
 * @since 2022-11-09
 */
@RestController
@Api(tags = "订单模块接口")
@RequestMapping("/order")
public class OrderController {

    @Resource
    private OrderService orderService;

    @PostMapping("/receiveDemand")
    @ApiOperation("接受需求生成订单")
    public DataUtils receiveDemand(@RequestBody OrderInput input){
        return orderService.receiveDemand(input);
    }

    @PostMapping("/updateOrderState")
    @ApiOperation("修改订单状态")
    public DataUtils updateOrderState(@RequestBody OrderInput input){
        return orderService.updateOrderState(input);
    }

    @GetMapping("/showWaitCreate/{userId}")
    @ApiOperation("获取待创作列表数据")
    public DataUtils showWaitCreate(@PathVariable Long userId){
        // 直接调用获取指定状态的Order
        List<Order> orderList = orderService.showStateOrderList(userId, OrderStateEnum.WAIT_CREATE.getState());
        return DataUtils.ok(orderList);
    }
    @GetMapping("/showWaitConfirm/{userId}")
    @ApiOperation("获取“待确认”数据")
    public DataUtils showWaitConfirm(@PathVariable Long userId){
        // 直接调用获取指定状态的Order
        List<Order> orderList = orderService.showStateOrderList(userId,OrderStateEnum.WAIT_CONFIRM.getState());
        return DataUtils.ok(orderList);
    }

    @GetMapping("/showCompleted/{userId}")
    @ApiOperation("获取“已完成”数据")
    public DataUtils showCompleted(@PathVariable Long userId){
        // 直接调用获取指定状态的Order
        List<Order> orderList = orderService.showStateOrderList(userId,OrderStateEnum.COMPLETED.getState());
        return DataUtils.ok(orderList);
    }

    @PostMapping("/createCompleted/{painterId}/{orderId}")
    @ApiOperation("创作完成")
    public DataUtils createCompleted(@PathVariable("painterId") Long painterId,@PathVariable("orderId") Long orderId){
        Boolean b = orderService.createCompleted(painterId,orderId);
        if(b) {
            return DataUtils.ok();
        }else {
            return DataUtils.error("不满足创作完成的条件");
        }
    }

    @PostMapping("/confirmAndEvaluate/{demanderId}/{orderId}")
    @ApiOperation("确认并评价")
    public DataUtils confirmAndEvaluate(@PathVariable("demanderId") Long demanderId, @PathVariable("orderId") Long orderId,
                                        @RequestBody Evaluate evaluate){
        Boolean b = orderService.confirmAndEvaluate(demanderId,orderId,evaluate);
        if(b) {
            return DataUtils.ok();
        }else {
            return DataUtils.error("操作失败");
        }
    }

    @GetMapping("/seePainterOrder/{painterId}")
    @ApiOperation("查询历史画师订单")
    public DataUtils seePainterOrder(@PathVariable("painterId") Long painterId){
        // 查询订单中画师是是 painterId 的订单 && 状态是已完成 && 已公开的
        LambdaQueryWrapper<Order> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(Order::getPainterId,painterId)
                .eq(Order::getState,OrderStateEnum.COMPLETED.getState())
                .eq(Order::getOpen, OrderOpenStateEnum.OPEN.getValue());

        List<Order> list = orderService.list(wrapper);
        // 由 Order => PainteHistoryOrderOutput
        List<PainteHistoryOrderOutput> result = list.stream().map(order -> {
            PainteHistoryOrderOutput dataItem = new PainteHistoryOrderOutput();
            BeanUtils.copyProperties(order, dataItem);
            return dataItem;
        }).collect(Collectors.toList());
        if(result != null) {
            return DataUtils.ok(result);
        }else {
            return DataUtils.error("操作失败");
        }
    }

    @GetMapping("/openShow/{orderId}")
    @ApiOperation("订单公开展示（订单转作品）")
    public DataUtils openShow(@PathVariable("orderId") Long orderId){
        // 修改订单为公开
        Boolean isOpenShowSuccess =  orderService.openShow(orderId);
        if(isOpenShowSuccess || isOpenShowSuccess == null) {
            return DataUtils.ok();
        }else {
            return DataUtils.error("公开失败");
        }
    }






}

